package uts.sdk.modules.debugUtsapi

import java.io.File
import java.io.FileOutputStream
import java.nio.ByteBuffer
import io.dcloud.uts.console

fun convertByteBufferToTempImage(
	buffer: ByteBuffer,
	fileExtension: String = "jpg",
	tempDirPath: String = "/storage/emulated/0/Android/data/io.dcloud.uniappx/cache/uni-media/"
): String {
	console.log(tempDirPath)
	// 确保从缓冲区开头读取数据
	buffer.rewind()
	// 将 ByteBuffer 转换为 ByteArray
	val bytes = ByteArray(buffer.remaining())
	buffer.get(bytes)
	// 将 ByteArray 写入临时文件
	val tempDir = File(tempDirPath)
	val tempFile = File.createTempFile(
		"temp_img_", 
		".${fileExtension.removePrefix(".")}",
		tempDir
	).apply {
		deleteOnExit()
	}
	FileOutputStream(tempFile).use {
		it.write(bytes)
	}
	return tempFile.absolutePath
}